<?php
/**
 * 职位管理
 *
 * @author Dracowyn
 * @since 2023-11-17 17:47
 */

// 引入配置文件
include_once('config/config.php');
$loginAuth = check_auth();

$page = $_GET['page'] ?? 1;

$limit = 10;

$countSql = "SELECT COUNT(`id`) AS count FROM `pre_job`";

$count = find($countSql);
$offset = ($page - 1) * $limit;
$jobSql =
	"SELECT job.id, job.name, dep.name AS depname, COUNT(person.id) AS cperson
	FROM pre_job job
         LEFT JOIN pre_department dep ON job.depid = dep.id
         LEFT JOIN pre_person person ON dep.id = person.depid
	GROUP BY job.id
	LIMIT $offset,$limit";
$jobData = all($jobSql);
$showPage = showPage($page, $count['count'], $limit);
?>

<html lang="zh">
<head>
	<?php include_once('common/meta.php'); ?>
	<title>职位管理</title>
</head>

<?php include_once('common/header.php'); ?>

<?php include_once('common/menu.php'); ?>

<div class="content">
	<div class="header">
		<h1 class="page-title">职位列表</h1>
	</div>
	<ul class="breadcrumb">
		<li><a href="index.php">首页</a><span class="divider">/</span></li>
		<li class="active">职位管理</li>
	</ul>

	<div class="container-fluid">
		<div class="row-fluid">
			<div class="btn-toolbar">
				<button class="btn btn-primary" onclick="location='jobAdd.php'"><i class="icon-plus"></i>添加职位
				</button>
			</div>
			<div class="well">
				<table class="table">
					<thead>
					<tr>
						<th>ID</th>
						<th>职位名称</th>
						<th>所属部门</th>
						<th>员工数量</th>
						<th style="width: 26px"></th>
					</tr>
					</thead>
					<tbody>
					<?php foreach ($jobData as $item) { ?>
						<tr>
							<td><?php echo $item['id']; ?></td>
							<td><?php echo $item['name']; ?></td>
							<td><?php echo $item['depname']; ?></td>
							<td><?php echo $item['cperson']; ?></td>
							<td>
								<a href="/jobEdit.php?id=<?php echo $item['id']; ?>"><i class="icon-pencil"></i></a>
								<a href="#myModal" role="button" data-toggle="modal"
								   data-id="<?php echo $item['id'] ?>"><i class="icon-remove"></i></a>
							</td>
						</tr>
					<?php } ?>
					</tbody>
				</table>
			</div>
			<div class="pagination">
				<?php echo $showPage; ?>
			</div>

			<div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
				 aria-hidden="true">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
					<h3 id="myModalLabel">确认删除</h3>
				</div>
				<div class="modal-body">
					<p class="error-text"><i class="icon-warning-sign modal-icon"></i>你确定删除该部门？</p>
				</div>
				<div class="modal-footer">
					<button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
					<button class="btn btn-danger" data-dismiss="modal">删除</button>
				</div>
			</div>

			<?php include_once('common/footer.php'); ?>
		</div>
	</div>
</div>
<?php include_once('common/script.php'); ?>
<script>
    let id = 0;
    $('a[href="#myModal"]').click(function () {
        id = $(this).data('id');
    });

    $('.btn-danger').click(function () {
        $.ajax({
            type: 'post',
            url: 'ajax.php',
            data: {
                action: 'del',
                table: 'job',
                id,
                adminId: '<?php echo $loginAuth['id']; ?>'
            },
            dataType: 'json',
            success: function (res) {
                if (res) {
                    if (res.code === 1) {
                        alert(res.msg)
                        location.reload();
                    } else {
                        alert(res.msg);
                    }
                }
            }
        })
    })
</script>
</html>
